/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao.impl;

import model.Usuarios;
import dao.UsuariosDao;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import util.HibernateUtil;

/**
 *
 * @author rafael
 */
public class UsuariosDaoImpl implements UsuariosDao {

    @Override
    public void inserir(Usuarios usuarios) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.beginTransaction();
            session.save(usuarios);
            session.beginTransaction().commit();
            GrowMessages.infoMessage(usuarios.getApelido(), "Inserido com sucesso");
        } catch (HibernateException e) {
            session.beginTransaction().rollback();
            GrowMessages.errorMessage(usuarios.getApelido(), "Erro ao inserir");
        } finally {
            session.close();
        }
    }

    @Override
    public void atualizar(Usuarios usuarios) {
        Session session = HibernateUtil.getSessionFactory().openSession();

        try {
            session.beginTransaction();
            session.update(usuarios);
            session.beginTransaction().commit();
            GrowMessages.infoMessage(usuarios.getApelido(), "Atualizado com sucesso");
        } catch (HibernateException e) {
            session.beginTransaction().rollback();
            GrowMessages.errorMessage(usuarios.getApelido(), "Erro ao atualizar");
            System.out.println(e.getLocalizedMessage());
        } finally {
            session.close();
        }

    }

    @Override
    public void eliminar(Usuarios usuarios) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.beginTransaction();
            session.delete(usuarios);
            session.beginTransaction().commit();
            GrowMessages.infoMessage(usuarios.getApelido(), "Eliminado com sucesso");
        } catch (HibernateException e) {
            session.beginTransaction().rollback();
            GrowMessages.errorMessage(usuarios.getApelido(), "Erro ao eliminar");
            System.out.println(e.getLocalizedMessage());
        } finally {
            session.close();
        }
    }

    @Override
    public Usuarios selectUser(String username, String password) {
        Session session = HibernateUtil.getSessionFactory().openSession();

        Criteria criteria = session.createCriteria(Usuarios.class);
        criteria.add(Restrictions.eq("login", username));
        criteria.add(Restrictions.eq("senha", password));
        try {

            return (Usuarios) criteria.uniqueResult();

        } catch (HibernateException e) {
            GrowMessages.errorMessage(e.getLocalizedMessage(), "Erro");
            System.out.println(e.getLocalizedMessage());
            return null;
        } finally {
            session.close();
        }
    }

    @Override
    public Usuarios selectUser(String username) {
        Session session = HibernateUtil.getSessionFactory().openSession();

        Criteria criteria = session.createCriteria(Usuarios.class);
        criteria.add(Restrictions.eq("login", username));
        try {

            return (Usuarios) criteria.uniqueResult();

        } catch (HibernateException e) {
            GrowMessages.errorMessage(e.getLocalizedMessage(), "Erro");
            System.out.println(e.getLocalizedMessage());
            return null;
        } finally {
            session.close();
        }
    }
}
